//
//  VKMeJiFenChildController.m
//  V-Learn
//
//  Created by liubo on 2019/1/9.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKReManagerChildController.h"
#import "VKReManagerChildCell.h"
#import "VKReManagerChildModel.h"
@interface VKReManagerChildController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger pageNums;
@property(nonatomic,assign)NSInteger selectIndex;
@end

@implementation VKReManagerChildController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      NSString *index =  [[NSUserDefaults standardUserDefaults] objectForKey:@"EarnRecordIndex"];
    self.selectIndex = [index integerValue];
    [self setupRefresh];
//    NSLog(@"%ld",self.selectIndex);
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - 285) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self tableView];
    self.selectIndex = 0;
    
}
//刷新
- (void)setupRefresh
{   //下拉
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupLoadData)];
    //上拉
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(setupLoadMoreData)];
    
    [self.tableView.mj_header beginRefreshing];
}
- (void)setupLoadData{
    _page = 1;
    _pageNums = 10;
    [self setupData];
}
/**
 加载更多数据
 */
- (void)setupLoadMoreData{
    
    ++(_page);
    _pageNums = 10;
    [self setupData];
}//决定footer的状态
- (void)checkFooterState{
    
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}
- (void)setupData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];
    NSMutableDictionary *dic = @{@"token":[UserModel getUserModel].token,@"page":[NSString stringWithFormat:@"%ld",(long)_page],@"num":[NSString stringWithFormat:@"%ld",(long)_pageNums]}.mutableCopy;
    VKWeakSelf(self)
    
    NSString *urlString;
    if (self.selectIndex==0) {
        urlString = WKQueryEarnRecord_WKURL;
        self.selectIndex = 0;
    }else{
        urlString =  WKQueryWithdraw_WKURL;
        self.selectIndex = 1;
    }
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:urlString withPostParams:dic.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            if (weakself.page == 1) {
                [self.dataArray removeAllObjects];
            }
            
            for (NSDictionary *dic in result[@"data"]) {
                VKReManagerChildModel *model = [VKReManagerChildModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            if ([result[@"data"] count] < 10) {
                //不能加载更多数据
                [self checkFooterState];
                [MBProgressHUD showSuccess:@"已加载全部数据" toView:self.view];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
        [weakself.tableView reloadData];
//        NSLog(@"%ld",self.selectIndex);
        [self.tableView.mj_header endRefreshing];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKReManagerChildCell *cell = [VKReManagerChildCell cellWithTableView:tableView];    cell.selectIndex = self.selectIndex;
    cell.model = self.dataArray[indexPath.row];
//    NSLog(@"%ld",self.selectIndex);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
